/**用于下载文件*/
define(["jquery","ecp.utils.i18n"],function($,i18n){
	var HTML_URL = "/grm/ecp/webcore/components/export/exportExt.html?expUrl=";
	var download =  function(servlet,data,fnm,ftp,opt){
		var inl = opt && opt.inline,
			intervalMs = opt.intervalMs,
			isTmpFile = opt && opt.isTmpFile;
		intervalMs = intervalMs ? intervalMs : 3000;
		function _buildIFrame(path){
			var  _url = servlet+"?path="+path+
		 			 "&fileName=" +  encodeURI(fnm) + //servlet中应设置 req.setCharacterEncoding("utf-8");
		 			 "&fileType="+ftp;        				   
			  if(inl || typeof ftp === "string" && ftp.toUpperCase() != "XLS") {
				  _url += "&inline=" + inl;
			  }
			  if(isTmpFile){
				  _url += "&isTmpFile=true";
			  }
			  downLoadFile(_url);
		}
		function downLoadFile(url){
			 if(url && url.indexOf( location.protocol ) < 0){
				 _url =  location.protocol +"//"+ location.host + url; 
			 }
			if (!window.externalHost) {
				var doc = document;
				dlf = doc.getElementById("fileDownloadFrame");
				if (dlf == null) {
					dlf = doc.createElement("iframe");
					dlf.id = "fileDownloadFrame";
					dlf.style.display="none";
					doc.body.appendChild(dlf);
				}
				dlf.src = HTML_URL + _url;
			} else {
				var win = window.open( HTML_URL + _url,i18n.exportExcel.exportNm );
			}
		}
		//如果数据量太大是否需要分割分批上传，服务器端生成上传数据的临时文件
		// data不是必须的
		var  sz,
			 bsz = 64000,    		//批次上传字节数64k
			 bc,
			 noData = false,
			 bd,
			 _url,
			 path, //服务器返回的临时文件名
			 eof,
			 _tmpPath,
			 _param; 
	
			_url = servlet+"?sno=-1"; //上传有sno参数,-1表示结束
			if(path){//第1次没有文件 名，以后则有 
				_url +="&path="+path;
			}
			 if(isTmpFile){
				 _url += "&isTmpFile=true";
			 }
			
			if(opt && opt.params){
				_param = opt.params;
				_param.contentPath = path; //contentPath是保留的参数名
				_tmpPath = _param.progressKey; //
			}else{
				_param = undefined;
			}
			var rsp = $.ajax({url: _url, async: false,dataType:"text", 
								type:"post",
								data: {
									content: data,  //数据内容
								    contentType: "text", //text,json
								    contentPart: undefined, // null,head,body	
									exportBean: $.toJSON({
										 beanId: opt ? opt.beanId : undefined,
										 methodName: opt ? (opt.methodName || opt.method) : undefined,
										 params: _param //methodName之参数
									})
								}
						     });
			path = rsp.responseText;
			if(!rsp || !rsp.responseText || rsp.responseText.indexOf("exception:")==0){
				   var msg = path ? path.substring("exception:".length) : i18n.exportExcel.error;
				   console && console.log(msg);
				   return;
		   }else{
				_buildIFrame(rsp.responseText);
		   }
	}
	return download;
});